<?php

require_once(dirname(__FILE__) . '/../extensions/EEnvironment.php');

/**
 * This is an example Environment, for when you want to use a custom SERVER_VAR
 * or CONFIG_DIR, or want to use other than the predefined modes.
 *
 * If you use the extended class, don't forget to require and create it from
 * your bootstrap file instead of the base Environment class.
 */
class Environment extends EEnvironment {
	// Environment settings
	//  (extend Environment class if you want to change these)
	//relative to Environment.php
	const CONFIG_DIR = '../config/';
	// for writing environment mode to file
	const MODE_FILE = '../config/mode.php';

	/**
	 * Get current environment mode depending on environment variable.
	 * If server environment not set, assumed production mode.
	 * @param string $mode
	 * @param bool $getModeByFile determine mode by file in config directory
	 * @return string
	 */
	protected function getMode($mode = null, $getModeByFile = false) {
		// If not overridden
		if ($mode === null) {
			if ($getModeByFile) {
				// Return mode based on certain file content
				if (!file_exists($this->getModeFile())) $mode = 'PRODUCTION';
				else $mode = file_get_contents($this->getModeFile());
			} else {
				$var = constant(get_class($this) . '::SERVER_VAR');
				// Return mode based on Apache server var
				if (!isset($_SERVER[$var])) $mode = 'PRODUCTION';
				else $mode = $_SERVER[$var];
			}
			$mode = strtoupper($mode);
		}

		// Check if mode is valid
		if (!defined(get_class($this) . '::MODE_' . $mode)) {
			$s = 'Invalid Environment mode supplied or selected';
			throw new Exception($s);
		}
		return $mode;
	}

}